/****************************************************************************
 * Copyright (C) 2012 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: MAXQ1740 timed delay
 *             $Id: maxq622_delay.c 3409 2012-03-11 18:19:14Z jbrodt $
 *
 ********************************************************************************
 */

#include "maxq_config.h"

/***** Definitions *****/
#define TIMER_ENABLED   0x04
#define OVERFLOW_FLAG   0x80


/***** Function Prototypes *****/


/***** Global Data *****/


/****************************************************************************
* Description: Perform the specified delay by using timer 1-1
*              If timer 1-1 is in use, function returns immediately (no delay)
****************************************************************************/
int delay_ms(unsigned int ms)
{
  int count = 0;

  // Check if timer already in use
  if (TB1CN & TIMER_ENABLED)
    return MAXQ_FAILURE;

  // Configure timer
  TB1CN = 0x0200; // auto-reload timer, count clock is /16, interrupt disabled
  TB1R = 0x4E2;  // count clock is 20MHz / 16 = 1.25MHz, 1.25MHz * 1ms = 0x4E2
  TB1CN |= TIMER_ENABLED;   // start timer

  // Perform delay
  while (count < ms) {
    while(!(TB1CN & OVERFLOW_FLAG));  // wait for overflow
    TB1CN &= ~OVERFLOW_FLAG;  // clear overflow flag
    count++;
  }

  // Stop timer
  TB1CN &= ~TIMER_ENABLED;

  return MAXQ_SUCCESS;
}

/****************************************************************************
* Description: Perform the specified delay by using timer 1-1
*              If timer 1-1 is in use, function returns immediately (no delay)
****************************************************************************/
int delay_us(unsigned int us)
{
  int count = 0;

  // Check if timer already in use
  if (TB1CN & TIMER_ENABLED)
    return MAXQ_FAILURE;

  // Configure timer
  TB1CN = 0x0000; // auto-reload timer, count clock is /1, interrupt disabled
  TB1R = 20;  // count clock is 20MHz * 1us = 20
  TB1CN |= TIMER_ENABLED;   // start timer

  // Perform delay
  while (count < us) {
    while(!(TB1CN & OVERFLOW_FLAG));  // wait for overflow
    TB1CN &= ~OVERFLOW_FLAG;  // clear overflow flag
    count++;
  }

  // Stop timer
  TB1CN &= ~TIMER_ENABLED;

  return MAXQ_SUCCESS;
}
